﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Filterpaper : BaseObj
{
    public GameObject m_Target;
    public GameObject m_BigTarget;
    public Animation m_Animation;

    public static Filterpaper Instance;
    [HideInInspector]
    public int state;//0没有折1折一次2折两次3折好了

    private void Awake()
    {
        Instance = this;
    }
    public override void OnHandleTrigger(Transform camera, NoloDeviceType handleType)
    {
        base.OnHandleTrigger(camera, handleType);
        transform.rotation = Quaternion.Euler(-90, 0, 0);
    }

    public override void OnReleseTrigger(NoloDeviceType handleType)
    {
        if (!InFunnel)
        {
            base.OnReleseTrigger(handleType);
            transform.SetParent(m_BigTarget.transform);
        }
        else
        {
            SetInFunnel();//放置到铁架台上
        }
        isCatch = false;
    }
    public void SetInFunnel()
    {
        m_rigidbody.useGravity = false;
        m_rigidbody.isKinematic = true;
        transform.position = m_Target.transform.position;
        transform.SetParent(m_Target.transform);
        StepsManager.Instance.StepTwoNum[1] = true;
        StepsManager.Instance.SetStepTwo(1);
    }
    private void Update()
    {
        if (isCatch)
        {
            if (NoloVR.NoloVRInput.singleton.OnNoloLeftTouchDown() || NoloVR.NoloVRInput.singleton.OnNoloRightTouchDown())
            {
                if (state == 0 && !m_Animation.isPlaying)
                {
                    m_Animation.Play("Paper1");
                    state = 1;
                }
                else if (state == 1 && !m_Animation.isPlaying)
                {
                    m_Animation.Play("Paper2");
                    state = 2;
                }
                else if (state == 2 && !m_Animation.isPlaying)
                {
                    m_Animation.Play("Paper3");
                    state = 3;
                    StepsManager.Instance.StepTwoNum[0] = true;
                    StepsManager.Instance.SetStepTwo(0);
                }
            }
        }
    }


    public bool InFunnel;
    private void LateUpdate()
    {
        if (Vector3.Distance(m_Target.transform.position, transform.position) < 0.1f && state == 3)
        {
            InFunnel = true;
        }
        else
        {
            InFunnel = false;
        }
    }


    private void OnCollisionStay(Collision other)
    {
        if (StepsManager.Instance.StepFourNum[1] && !StepsManager.Instance.StepFiveNum[3])
        {
            StepsManager.Instance.StepFiveNum[3] = true;
            StepsManager.Instance.SetStepFive(3);
        }
    }
}
